
// Job
//
// 保佑没Bug
//
// Created by AS on 2018/12/28.
//
// Copyright © 2018 AS. All rights reserved.
//

#import "JoBDetailsViewController.h"
#import "MapViewController.h"
@interface JoBDetailsViewController ()
/**  */
@property (nonatomic,strong) NSDictionary *dict;

/**  */
@property (nonatomic,strong) UIView *tabbarView;
/**  */
@property (nonatomic,strong) UIButton *button;
@end

@implementation JoBDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"职位详情";
    
    self.webView.frame = CGRectMake(0, 0, ScreenWidth, self.view.height - TabBarHeight - TopHeight);
    
    self.tabbarView = [[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.webView.frame), ScreenWidth, TabBarHeight)];
    [self.view addSubview:self.tabbarView];
    
    self.button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 49)];
    self.button.hidden = YES;
    [self.button setTitle:@"立即报名" forState:UIControlStateNormal];
    [self.button setBackgroundImage:[UIImage createImageWithColor:BaseThemeColor] forState:UIControlStateNormal];
    [self.button setBackgroundImage:[UIImage createImageWithColor:HexRGB(0xdddddd)] forState:UIControlStateDisabled];
    [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(signAction) forControlEvents:UIControlEventTouchUpInside];
    [self.tabbarView addSubview:self.button];
    
    
    [self webViewLoadUrl:@"/www/html/jobdetails"];
    
}

-(void)signAction{
    
    XLAuthModel *user = [XLAuthModel getAuth];
    if (!user) {
        UIViewController *login = SBView(@"Login", @"LoginViewController");
        [self pushViewController:login];
    }else{
        
        if([self.dict[@"type"] intValue] == 0){
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            dict[@"user_id"] = user.userID;
            NSString *url = [NSString stringWithFormat:@"/api/v2/jobs/%@",self.dict[@"id"]];
            MBProgressHUD *hud = [MBProgressHUD showMessage:@"报名申请中..."];
            [HTTPTools GET:url parameters:dict callback:^(id result, NSString *msg, NSError *error) {
                [hud hideAnimated:YES];
                if (!error) {
                    if (!msg) {
                        NSMutableDictionary *data = [NSMutableDictionary dictionaryWithDictionary:self.dict];
                        data[@"type"] = @(1);
                        self.dict = data;
                        [self.button setTitle:@"已报名" forState:UIControlStateNormal];
                        self.button.enabled = NO;
                    }
                }
            }];
        }
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getJobDetails];
}


-(void)getJobDetails{
    
    
    NSString *url = [NSString stringWithFormat:@"/api//v4/jobs/%ld",self.model.jobId];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    XLAuthModel *user = [XLAuthModel getAuth];
    if (user) {
        dict[@"user_id"] = user.userID;
    }
    WeakSelf(weakSelf);
    MBProgressHUD *hud = [MBProgressHUD showMessage:@""];
    [HTTPTools GET:url parameters:dict callback:^(id result, NSString *msg, NSError *error) {
        [hud hideAnimated:YES];
        if (!error) {
            if (!msg) {
                weakSelf.dict = result;
                weakSelf.button.hidden = NO;
                [weakSelf.bridge callHandler:@"registerData" data:[result mj_JSONString]];
                if ([result[@"type"] intValue] == 1) {
                    [weakSelf.button setTitle:@"已报名" forState:UIControlStateNormal];
                    weakSelf.button.enabled = NO;
                }
            }
        }
    }];
    
    [self.bridge registerHandler:@"mapAction" handler:^(id data, WVJBResponseCallback responseCallback) {
        
        MapViewController *view = [MapViewController new];
        view.latitude = [weakSelf.dict[@"latitude"] doubleValue];
        view.longitude = [weakSelf.dict[@"longitude"] doubleValue];
        view.name = weakSelf.dict[@"companyName"];
        [weakSelf pushViewController:view];
    }];
    
}




@end
